/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DataConverters.DataReaders.readersImpl;

import DataConverters.DataReaders.AbstractDataReader;
import DataConverters.exceptions.DataReaderException;
import DataConverters.interfaces.IConvertable;
import common.gui.progress.IProgressItem;
import DataConverters.interfaces.dataformulaobject.impl.DefaultCellBuilder;
import DataConverters.interfaces.dataformulaobject.intefaces.CellBuilder;
import FileInformation.FileInformation;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author Nobody
 */
public class XlsxReader extends AbstractDataReader {

    public XlsxReader(IProgressItem progressLabel, IConvertable source) {
        super(progressLabel, source);
    }

    @Override
    public void read() throws DataReaderException {
        try {
            FileInformation fileInformation = (FileInformation) source;
            XSSFWorkbook workBook = new XSSFWorkbook(fileInformation.getFile().getAbsolutePath());
            XSSFSheet sheet = workBook.getSheet(fileInformation.getChoosedSheetName());
            if (sheet == null) {
                sheet = workBook.getSheet(fileInformation.getExcelSheetNames()[0]);
            }

            readFromSheet(sheet);
        } catch (IOException ex) {
            throw new DataReaderException(ex);
        }

    }

    private void createFormulaObjectFromCell(XSSFCell currentCell, int j, int i) {
        CellBuilder cellBuilder = new DefaultCellBuilder();
        switch (currentCell.getCellType()) {
            case XSSFCell.CELL_TYPE_BOOLEAN:
                formulasList.add(cellBuilder.createBooleanCell(i, j, currentCell.getBooleanCellValue()));
            case XSSFCell.CELL_TYPE_ERROR:
                formulasList.add(cellBuilder.createLabelCell(i, j, currentCell.getErrorCellString()));
            case XSSFCell.CELL_TYPE_BLANK:
                formulasList.add(cellBuilder.createLabelCell(i, j, ""));
                break;
            case XSSFCell.CELL_TYPE_NUMERIC:
                formulasList.add(cellBuilder.createNumberCell(i, j, currentCell.getNumericCellValue()));
                break;
            case XSSFCell.CELL_TYPE_STRING:
                formulasList.add(cellBuilder.createLabelCell(i, j, currentCell.getStringCellValue()));
                break;
            case XSSFCell.CELL_TYPE_FORMULA:
            /*
            try {
            /* if we invoke getNumericCellValue we can get IllegalStateException if
             * cell value is not of number type. Then its mean that value must be of
             * string type (maybe data

            double data = currentCell.getNumericCellValue();
            formulasList.add(new DefaultDataFormulaObject(j, i, "=" + currentCell.getCellFormula(), data));
            } catch (IllegalStateException ex) {

            String data = currentCell.getStringCellValue();
            formulasList.add(new DefaultDataFormulaObject(j, i, "=" + currentCell.getCellFormula(), data));
            }*/
        }
    }

    private void readFromSheet(XSSFSheet sheet) {
        progressItem.beforeReading();
        progressItem.setMaximum(sheet.getPhysicalNumberOfRows());
        int progress = 0;

        for (int i = 0; i < sheet.getPhysicalNumberOfRows(); ++i) {
            for (int j = 0; j < sheet.getRow(i).getPhysicalNumberOfCells(); ++j) {
                XSSFCell currentCell = sheet.getRow(i).getCell(j);
                createFormulaObjectFromCell(currentCell, j, i);
            }
            progressItem.readed(++progress);
        }
        progressItem.afterReading();
    }
}
